package com.example.netty.netty.TCPExample;

import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.ssl.SslContext;
import io.netty.handler.ssl.SslHandler;
import io.netty.util.Attribute;
import io.netty.util.AttributeKey;

import javax.net.ssl.SSLEngine;

public class NettyClient {
    public static void main(String[] args) throws Exception {
        NioEventLoopGroup eventExecutors = new NioEventLoopGroup(2);
        try {
            //与server端的不一样
            Bootstrap bootstrap = new Bootstrap();
            bootstrap.group(eventExecutors)
                    .channel(NioSocketChannel.class)
                    .handler(new NettyClientHandler());

            for (int i = 0; i < 3; i++) {
                ChannelFuture future = bootstrap.connect("127.0.0.1", 6668).sync();

                future.channel().closeFuture().sync().addListener(new ChannelFutureListener() {
                            @Override
                            public void operationComplete(ChannelFuture future) throws Exception {
                                //关闭channel就可以，不能关闭eventLoop或者eventLoopGroup
                                System.out.println("channel:"+future.channel().hashCode()+"已关闭");
                            }
                        });
                System.out.println("第"+(i+1)+"个客户端启动成功。。。。");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            eventExecutors.shutdownGracefully();
        }
    }
}
